#!/bin/sh

# Slackware build script for MPlayer

# Copyright 2006-2007  Robby Workman  (http://rlworkman.net)
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Thanks to Niki Kovaks for some suggestions on this 
# Thanks to Eric Hameleers for suggestions (and some code examples) :)

PRGNAM=MPlayer
VERSION=1.0rc1	
PKGVERSION=$VERSION	# This may change later in the script
ARCH=${ARCH:-custom}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# If you installed the codecs package according to the instructions at
# the MPlayer website, you'll probably want to change this variable
# to /usr/local/lib/codecs.  Otherwise, you'll want to leave it alone
# (if you installed the codecs using our SlackBuild script for them)
# unless you placed the codecs in a non-standard location.
CODECSDIR=${CODECSDIR:-/usr/lib/codecs}

# The default MPlayer theme is Blue
# http://www.mplayerhq.hu/MPlayer/skins/Blue-1.7.tar.bz2
# Others may work fine here, but the script has not been tested with them
# Even so, it's probably better to leave this alone and install additional
# themes to $HOME/.mplayer/skins/
THEME=Blue
THEME_VERSION=1.7

# The fonts listed below should exist on a stock Slackware 11.0 system.
# If you want to change the default font after installation, that is probably 
# best done by making a link to your preferred font in $HOME/.mplayer/ by:
#   cd $HOME/.mplayer
#   ln -s /path/to/preferred/font subfont.ttf 
OSDFONTS="Arialuni.ttf arial.ttf DejaVuSans.ttf Vera.ttf"

# This will build documentation for all languages - if you don't want some
# of the, either change this or pass what you do want on the command line
# when running the script:  LANGUAGES=en ./MPlayer.SlackBuild 
LANGUAGES=${LANGUAGES:-"en nl fr de es"}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP || exit 1
rm -rf $PRGNAM-$VERSION
tar -xjvf $CWD/$PRGNAM-$VERSION.tar.bz2 || exit 1
cd $PRGNAM-$VERSION || exit 1
chown -R root:root .
chmod -R a-s,u+rw,go+r-w .

# Fix a security problem and increment package version to try2
# http://www.debian.org/security/2006/dsa-1244
patch -p0 < $CWD/asmrules_fix_20061231.diff || exit 1
PKGVERSION=${VERSION}try2

# Fix a security problem and increment package version to try3
# http://secunia.com/product/2255/
# http://svn.mplayerhq.hu/mplayer/trunk/stream/stream_cddb.c?r1=23287&r2=23470&diff_format=u
patch -p1 < $CWD/stream.cddb_fix_20070606.diff || exit 1
PKGVERSION=${VERSION}try3

# The MPlayer developers will not support and/or deal with bug reports
# if you don't allow it to automatically determine compiler flags,
# so modify this at your own risk
CFLAGS= \
./configure \
  --prefix=/usr \
  --confdir=/etc/mplayer \
  --enable-gui \
  --enable-menu \
  --enable-runtime-cpudetection \
  --enable-largefiles \
  --with-codecsdir="$CODECSDIR" \
  --language="$LANGUAGES" \
  || exit 1

make || exit 1
make install DESTDIR=$PKG || exit 1

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/usr/share/mplayer/skins
if [ -r $CWD/$THEME-$THEME_VERSION.tar.bz2 ]; then
  ( cd $PKG/usr/share/mplayer/skins
    tar -xjf $CWD/$THEME-$THEME_VERSION.tar.bz2
    ln -s $THEME default
  ) || exit 1
else
  echo A usable theme tarball does not appear to exist in $CWD
  echo \(or you didn't edit the script to reflect it, or something
  echo else is broken\)
  echo The script will continue to build a package, but some manual
  echo setup may be required after installation.
  echo
  echo Type \'exit\' to quit or any other key to continue the script.
  read _continue
  if [ "$_continue" = "exit" ]; then
      exit 1
  fi
fi

# Fix path to icon in desktop file so that the icon will display in kde
sed -i 's%Icon=mplayer.xpm%Icon=/usr/share/pixmaps/mplayer.xpm%' \
  $PKG/usr/share/applications/mplayer.desktop || exit 1

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS ChangeLog Copyright LICENSE README DOCS/* \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Don't clobber an existing config file
cat etc/example.conf > $PKG/etc/mplayer/mplayer.conf.new

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# We'll link to one of the standard ttf fonts defined above
# Thanks to Eric Hameleers for this code snippet
cat << EOF >> $PKG/install/doinst.sh
# Symlink a default TrueType font for OSD:
if [ ! -f usr/share/mplayer/subfont.ttf ]; then
  for font in $OSDFONTS ; do
    if [ -f usr/X11R6/lib/X11/fonts/TTF/\$font ]; then
      ( cd usr/share/mplayer 
        ln -fs /usr/X11R6/lib/X11/fonts/TTF/\$font subfont.ttf 
      )
      break
    fi
  done
fi
EOF

cd $PKG
chown -R root:root .	# Some files in /usr/share/mplayer are 103:99
/sbin/makepkg -l y -c n -p $OUTPUT/$PRGNAM-$PKGVERSION-$ARCH-$BUILD$TAG.tgz
